<?php

/*------------------------------------------------
*
*   Explay Engine Content Management System.
*   Copyright © 2008 Golovdinov Alexander.
*
*-------------------------------------------------
*
*   Official site: wwww.alex-home.spb.ru/explay.html
*   Contact e-mail: golovdinov@gmail.com
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
------------------------------------------------*/

if (!defined ('EXPLAY')) exit;

class MySQL {
	
	private $count_query = 0;
	private $work_time = '';
	private $query_id = false;
	private $time_before = 0;
	private $mysql_version = 0;
	private $mode;
	
	private $dbserver;
	private $dbuser;
	private $dbpass;
	private $dbname;
	
	private static $instance = false;
	public static function getInstance () {
		if (self::$instance === false) self::$instance = new MySQL;
			
		return self::$instance;
	}
	
	private function __construct (/*$dbserver, $dbuser, $dbpass, $dbname, $mode = 'ajax'*/) {
		$this->dbserver = MySQL_SERVER; //$dbserver;
		$this->dbuser = MySQL_USER; //$dbuser;
		$this->dbpass = MySQL_PASSWORD; //$dbpass;
		$this->dbname = MySQL_DB_NAME; //$dbname;
		$this->mode = MySQL_MODE; //$mode;
	}

	public function connect () {
		$mysqlconnect = mysql_connect ($this->dbserver, $this->dbuser, $this->dbpass);
		if (!$mysqlconnect)
			exit ('Невозможно подключиться к серверу баз данных!');
			
		$mysqlselect = mysql_select_db ($this->dbname, $mysqlconnect);
		if (!$mysqlselect)
			exit ('Невозможно подключиться к базе данных!');
		
		$this->mysql_version = mysql_get_server_info();

		if (version_compare($this->mysql_version, '4.1', ">=")) mysql_query("/*!40101 SET NAMES 'utf8' */");
		
		/*mysql_query ("set character_set_client='cp1251'");
		mysql_query ("set character_set_results='cp1251'");
		mysql_query ("set collation_connection='Fuck me cp1251_general_ci'");*/
	}
	
	public function query ($string) {
		$time_before = 0;
		$time_before += $this->get_time();
		$this->count_query++;
		
		$this->last_query = $string;
		
		try {
			if (!$this->query_id = mysql_query ($string))
				throw new Exception;
		}
		catch (Exception $e) { // Ловим исключение и узнаем в каком файле и на какой строке вызван данный метод. Неплохо да?
			$err = $e->getTrace();
			myErrorHandler(mysql_errno(), mysql_error(), $err[0]['file'], $err[0]['line'], '');
		}
		
		$this->work_time += $this->get_time() - $time_before;
		
		return $this->query_id;
	}
	
	public function fetch_array ($value) {
		$time_before = 0;
		$time_before += $this->get_time();
		
		$this->work_time += $this->get_time() - $time_before;
		return mysql_fetch_array ($value);
	}
	
	public function fetch_row ($value) {
		$time_before = 0;
		$time_before += $this->get_time();
		
		$this->work_time += $this->get_time() - $time_before;
		return mysql_fetch_row ($value);
	}
	
	public function num_rows ($value) {
		$time_before = 0;
		$time_before += $this->get_time();
		
		$this->work_time += $this->get_time() - $time_before;
		return mysql_num_rows ($value);
	}
	
	private function log_error ($error, $string) {
		
		$dt = date("Y-m-d H:i:s");
		$myip=getenv('REMOTE_ADDR');
		$err = "<error>\r\n";
		$err .= '<date>' . $dt . "</date>\r\n";
		$err .= '<ip>' . $_SERVER['REMOTE_ADDR'] . "</ip>\r\n";
		$err .= "<errno>13</errno>\r\n";
		$err .= "<type>MySQL error</type>\r\n";
		$err .= '<msg>' . $error . ' <br /> ' . $string . "</msg>\r\n";
		$err .= '<urlstring>' . $_SERVER['REQUEST_URI'] . "</urlstring>\r\n";
		$err .= "<script>none information</script>\r\n";
		$err .= "<line>none information</line>\r\n";
		$err .= "</error>\r\n";
		
		log_error($err);
		ob_end_clean ();
		if ($this->mode == 'html') include '500.html';
		exit();
	}
	
	public function past_work_time () {
		return $this->work_time;
	}
	public function past_count_queries () {
		return $this->count_query;
	}
	
	private function get_time()
	{
		$this_time = microtime(); 
		$time_array = explode(' ',$this_time); 
		return ($time_array[1] + $time_array[0]); 
	}

}
